package com.classroom.smart.controller;

import com.classroom.smart.result.CommonResult;
import com.classroom.smart.constant.MessageConstant;
import com.classroom.smart.pojo.User;
import com.classroom.smart.result.Result;
import com.classroom.smart.service.UserService;
import com.classroom.smart.utils.OSSUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping({"/SignUp"})
public class UserController {

    @Autowired
    private UserService userService;

    //查询用户列表
    @GetMapping("viewUser")
    @ResponseBody
    public CommonResult viewAll(
            @RequestParam(value = "current" , required = false , defaultValue = "1") int current ,
            @RequestParam(value = "query" , required = false , defaultValue = "") String query
    ){
        //查询用户列表
        List<User> users = userService.viewAllByQuery(current, query);
        CommonResult commonResult = null;
        if(users!=null && users.size()!=0){
            //封装PageInfo对象
            PageInfo<User> pageInfo = new PageInfo<>(users);
            //将PageInfo、查询条件一起封装至Map集合中
            HashMap<String,Object> map = new HashMap<>();
            map.put("pageInfo",pageInfo);
            map.put("query",query);
            commonResult = new CommonResult(200 , "查询成功" , map);
        }else{
            commonResult = new CommonResult(477 , "查询失败");
        }
        return commonResult;
    }

    //注册  上传图片
    @PostMapping("submit")
    public CommonResult submit(User user , @RequestPart("imgFile")MultipartFile imgFile) throws IOException {
        try{
            if (imgFile!=null){
                String oldName = imgFile.getOriginalFilename();
                if (StringUtils.hasLength(oldName)){
                    String newName = UUID.randomUUID()+oldName.substring(oldName.lastIndexOf("."));
                    //上传操作
                    String fileName = OSSUtils.upload(newName, imgFile.getInputStream());
                    //图片上传成功
                    return new CommonResult(200,"上传成功",fileName);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        CommonResult commonResult = null;
        if(user!=null){
            boolean flag = userService.insert(user);
            if(flag){
                commonResult = new CommonResult(200 , "注册成功" , flag);
            }else{
                commonResult = new CommonResult(455 , "注册失败");
            }
        }
        return commonResult;
    }

    //删除用户信息  @PathVariable可以解析路径变量，value属性与{Key}与Key一致即可
    @DeleteMapping("deleteUser/{userId}")
    @ResponseBody
    public CommonResult del(@PathVariable("userId") int userId){
        CommonResult commonResult = null;
        boolean flag = userService.delete(userId);
        if(flag){
            commonResult = new CommonResult(200 , "删除成功" , flag);
        }else{
            commonResult = new CommonResult(499 , "删除失败");
        }
        return commonResult;
    }

    //根据用户ID获取要更新的用户的信息
    @GetMapping("getUpdateUser/{userId}")
    @ResponseBody
    public CommonResult getUpdatas(@PathVariable int userId){
        CommonResult commonResult = null;
        User user = userService.viewOne(userId);
        if (user != null){
            commonResult = new CommonResult(200,"查询成功",user);
        }else {
            commonResult = new CommonResult(455,"查询失败");
        }
        return commonResult;
    }

    //更新用户信息
    @PutMapping("update")
    @ResponseBody
    public CommonResult updataUser(@RequestBody User user){
        CommonResult commonResult = null;
        boolean flag  = userService.update(user);
        if (flag){
            commonResult = new CommonResult(200,"更新成功",flag);
        }else {
            commonResult = new CommonResult(455,"更新失败");
        }
        return commonResult;
    }

}
